package cn.edu.swu.cly.io;

import java.io.IOException;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;
import java.util.Calendar;

public class WebDownloader {
    public void download(String strUrl, String targetFilePath) throws IOException {
        URL url = new URL(strUrl);
        URLConnection connection = url.openConnection();

        //和Filetools——copy一样
        long start = Calendar.getInstance().getTimeInMillis();      //输出运行时间

        try(InputStream inputStream = new FileInputStream(strUrl)) {        //输入源的内容是strurl
            try(OutputStream outputStream = new FileOutputStream(targetFilePath)) {
                int count;
                byte[] buffer = new byte[1024 * 4];
                while((count = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0 ,count);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        long end = Calendar.getInstance().getTimeInMillis();
        System.out.println("使用时间：" + (end - start));
    }
}
